package Stack;

public class Calculator {

  public static void main(String[] args) {
//    定义表达式
    String expression = "70+6*2-4";
//    定义栈
    ArrayStack2 numStack = new ArrayStack2(10);
    ArrayStack2 operStack = new ArrayStack2(10);
//    定义变量
    int res = 0;
    int num1 = 0;
    int num2 = 0;
    int oper = 0;
    char ch = ' ';
    int index = 0;
    String keepNum = "";

    while (true){
      ch = expression.substring(index,index + 1).charAt(0);
//      判断是操作数还是操作符
      if (operStack.isOper(ch)){
//        是操作符
//        先判断栈是否为空，不为空的话判断优先级
        if (!operStack.isEmpty()){
          if (operStack.prioirty(ch) <= operStack.prioirty(operStack.peek())){
            num1 = numStack.pop();
            num2 = numStack.pop();
            oper = operStack.pop();
            res = numStack.cal(num1,num2,oper);
//            把结果放回栈
            numStack.push(res);
//            操作符放入栈
            operStack.push(ch);
          }else {//当前符号优先级大于栈顶符号的优先级
            operStack.push(ch);
          }
        }else {//符号栈为空的情况
          operStack.push(ch);
        }

      }else {
//        当发现是数字时不能直接入栈
//        numStack.push(ch - 48);
        keepNum = keepNum +  ch;

        if (index == expression.length()-1){
          numStack.push(Integer.parseInt(keepNum));
        }else {
          if (operStack.isOper(expression.substring(index+1,index+2).charAt(0))){
            numStack.push(Integer.parseInt(keepNum));
//            别忘了把临时变量清空
            keepNum = "";
          }
        }
      }
      index++;
      if (index >= expression.length()){
        break;
      }
    }

    while (true){
      if (operStack.isEmpty()){
        break;
      }
      num1 = numStack.pop();
      num2 = numStack.pop();
      oper = operStack.pop();
      res = numStack.cal(num1,num2,oper);
      numStack.push(res);
    }
    System.out.println("表达式的值为：" + numStack.pop());
  }
}

class ArrayStack2{
  private int maxSize;
  private int top = -1;
  private int[] stack;

  public ArrayStack2(int maxSize){
    this.maxSize = maxSize;
    stack = new int[maxSize];
  }
  //  判空
  public boolean isEmpty(){
    return top == -1;
  }
  //  判满
  public boolean isFull(){
    return top == maxSize-1;
  }
  //  出栈
  public int pop(){
    if (isEmpty()){
      throw new RuntimeException("栈空");
    }
    int res = stack[top];
    top--;
    return res;
  }
  //  入栈
  public void push(int value){
    if (isFull()){
      System.out.println("满了");
    }
    top++;
    stack[top] = value;
  }
  //  显示数据
  public void list(){
    if (isEmpty()){
      System.out.println("空的");
    }
    for (int i = top; i >= 0; i--) {
      System.out.println(stack[i]);
    }
  }
//  查看栈顶的值
  public int peek(){
    return stack[top];
  }
//  判断是否是符号
  public boolean isOper(char value){
    return value == '+' || value == '-' || value == '*' || value == '/';
  }
//  判断符号优先级
  public int prioirty(int value){
    if(value == '*' || value == '/'){
      return 1;
    }
    if (value == '+' || value == '-'){
      return 0;
    }
    return -1;
  }
//  计算
  public int cal(int num1,int num2,int value){
    int res = 0;
    switch (value) {
      case '+':
        res = num1 + num2;
        break;
      case '-':
        res = num2 - num1;
        break;
      case '*':
        res = num1 * num2;
        break;
      case '/':
        res = num2 / num1;
        break;
      default:
        break;
    }
    return res;
  }
}
